import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

  const routes = [
  {
    path:'/productlist',
    name:"ProductList",
    component:()=>import(/* webpackChunkName: "productlist" */"../views/ProductList.vue")
  },
  {
    path:"/detail",
    name:"Detail",
    component:()=>import(/* webpackChunkName: "detail" */"../views/Detail.vue")
  },
  {
    path:"/cart",
    name:"Cart",
    component:()=>import(/* webpackChunkName: "cart" */"../views/Cart.vue")
  },
  {
    path:'/cart/edit',
    component:()=>import("../views/EditCart.vue")
  },
  {
    path:"/mine",
    name:"Mine",
    component:()=>import(/* webpackChunkName: "mine" */"../views/Mine.vue")
  },
  {
    path:"/login",
    name:"Login",
    component:()=>import(/* webpackChunkName: "login" */"../views/Login.vue")
  },
  {
    path:"/reg",
    name:"Reg",
    component:()=>import(/* webpackChunkName: "reg" */"../views/Reg.vue")
  },
  {
    path:"/search",
    name:"Search",
    component:()=>import(/* webpackChunkName: "search" */"../views/Search.vue")
  },
  {
    path:"/search/plist",
    component:()=>import("../views/SearchList.vue")
  },
  {
    //动态路由  首页切换
    path: "/silo/:id",
    component:()=>import(/* webpackChunkName: "reg" */"../views/homeView/Silo.vue"),
    props: true,
    name: 'silo'
    //命名路由 具名路由  :to="{ name:'detail',params:{id:data}}"  跳转页面
  },
  {
    // 首页
    path: "/index",
    component:()=>import(/* webpackChunkName: "reg" */"../views/homeView/Home.vue"),
  },
  {
    // upcoming
    path: "/upcoming",
    component:()=>import(/* webpackChunkName: "reg" */"../views/homeView/Upcoming.vue"),
  },
  {
    path: "/",
    redirect: "/index"  //重定向 ==>  /index
  },
]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path === "/cart"){
    if(localStorage.getItem("token")){
      next()
    }else{
      next({
        path:"/login"
      })
    }
  }else{
    next()
  }
})



export default router
